package vno.fux.character
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	import vno.fux.GlobalEventScheduler;
	import vno.fux.GlobalVar;
	import vno.fux.events.MapEvent;
	import vno.fux.events.NpcEvent;
	import vno.fux.mediator.LayerMediator;
	import vno.fux.CompassDirection;

	public class CharacterLayer extends Sprite
	{
		private var _NPCLayer:NpcLayer;
		private var _playerLayer:PlayerLayer;
		private var _currentMapId:uint;
		private var _settings:XML;
		public function CharacterLayer(layerSetting_:XML)
		{
			this.mouseEnabled = false;
			_settings = layerSetting_;
			GlobalEventScheduler.getInstance().addResizeListener(resizeHandler);
			//initPlayerLayer();
		}
		public function create(id_:uint):void
		{
			_currentMapId = id_;
			initNPCLayer();
			initPlayerLayer();
		}
		public function resizeHandler(event:Event):void
		{
			this.x = GlobalVar.stage.stageWidth/2;
			this.y = GlobalVar.stage.stageHeight/2;
		}
		public function handleMoveState(event:MapEvent):void
		{
			_playerLayer.moveStateHandle((event as MapEvent).type);
		}
		
		public function handleMapMove(event:MapEvent):void
		{
			_NPCLayer.mapMove((event as MapEvent).pos);
			_playerLayer.handleCover((event as MapEvent).roadValue);
			
		}
		public function handleDirection(event:MapEvent):void
		{
			_playerLayer.changeDirection((event as MapEvent).direction);
		}
		public function setMainPlayer(id_:uint, resId_:uint, direction_:String):void
		{
			_playerLayer.mainPlayer.set(id_, resId_, direction_);
		}
		private function initNPCLayer():void
		{
			_NPCLayer = new NpcLayer(_settings.map.(@id==_currentMapId).npcLayer);
			_NPCLayer.addEventListener(NpcEvent.CLICK, eventHandler);
			addChild(_NPCLayer);
		}		
		private function eventHandler(event:Event):void
		{
			dispatchEvent(event);
		}
		
		private function initPlayerLayer():void
		{
			_playerLayer = new PlayerLayer(_settings.map.(@id==_currentMapId).playerLayer);
			addChild(_playerLayer);
		}
	}
}